 package net.ruixin.orm.hibernate;

 import org.hibernate.criterion.Order;

 public class OrderBy
 {
   private OrderType orderType;
   private String field;

   public String getField()
   {
     return this.field;
   }

   public OrderBy(String field, OrderType orderType) {
     this.field = field;
     this.orderType = orderType;
   }

   public static OrderBy asc(String field) {
     return new OrderBy(field, OrderType.ASC);
   }

   public static OrderBy desc(String field) {
     return new OrderBy(field, OrderType.DESC);
   }

   public Order getOrder() {
     Order order = null;
     if (OrderType.ASC == this.orderType)
       order = Order.asc(getField());
     else if (OrderType.DESC == this.orderType) {
       order = Order.desc(getField());
     }
     return order;
   }

   public static Order[] asOrders(OrderBy[] orderBys) {
     if (orderBys != null) {
       Order[] orders = new Order[orderBys.length];
       for (int i = 0; i < orderBys.length; ++i) {
         orders[i] = orderBys[i].getOrder();
       }
       return orders;
     }
     return null;
   }

   public static enum OrderType
   {
     ASC, DESC;
   }
 }

